package com.situ.b2c.register.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.situ.b2c.pojo.User;
import com.situ.b2c.register.dao.UserDao;

public class UserService {
	UserDao userDao = new UserDao();
	
	
	public int saveUser(User user) {
		user.setCreateBy("admin");
		user.setCreateDate(new Date());
		return userDao.insert(user);
	}
	
	public int checkCode(String userCode) {
		int result = 0;
		User user = userDao.selectOneByCode(userCode);
		if (user ==null) {
			result = 1;
		}
		return result;
	}
	
	public int checkPass(String userOldPass,HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("login_user");
		System.out.println(user);
		System.out.println(user.getUserPass());
		int result = 0;
		if (user != null) {
		if (user.getUserPass().equals(userOldPass)) {
			result = 1;
		}
		}
		return result;
	}
	
	
	public User selectOneById(Long rowId) {
		
		return userDao.selectOneById(rowId);
		
	}

	public int update(User user) {
		Long rowId = user.getRowId();
		//System.out.println(rowId);
		User edituser = userDao.selectOneById(rowId);
		//System.out.println(edituser);
		if (edituser != null) {
			
		
		edituser.setUserName(user.getUserName());
		edituser.setUserCode(user.getUserCode());
		edituser.setUserPass(user.getUserPass());
		edituser.setUserAvatar(user.getUserAvatar());
		edituser.setUpdateBy("admin");
		edituser.setUpdateDate(new Date());
		
		}
		
		return userDao.update(edituser);
		
	}
	
}
